package com.health.userbackstage.service.impl;

import com.health.userbackstage.mapper.MemberMapper;
import com.health.userbackstage.pojo.HotSetmeal;
import com.health.userbackstage.service.BusinessService;
import com.health.userbackstage.utils.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Service
public class BusinessServiceImpl implements BusinessService {

    //注入会员相关信息mapper
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Map<String, Object> getData() throws InterruptedException {

        Map<String, Object> data = new HashMap<>();

        //并发包下的门闩对象，底层是AQS
        CountDownLatch cdl = new CountDownLatch(12);

        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long todayNewMember = memberMapper.todayNewMember();
                data.put("todayNewMember",todayNewMember);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long weekNewMember = memberMapper.weekNewMember();
                data.put("thisWeekNewMember",weekNewMember);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long monthNewMember = memberMapper.monthNewMember();
                data.put("thisMonthNewMember",monthNewMember);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long memberCount = memberMapper.memberCount();
                data.put("totalMember",memberCount);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                String today = memberMapper.today();
                data.put("reportDate",today);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long todayOrderNumber = memberMapper.todayOrderNumber();
                data.put("todayOrderNumber",todayOrderNumber);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long todayVisitsNumber = memberMapper.todayVisitsNumber();
                data.put("todayVisitsNumber",todayVisitsNumber);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long thisWeekOrderNumber = memberMapper.thisWeekOrderNumber();
                data.put("thisWeekOrderNumber",thisWeekOrderNumber);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long thisWeekVisitsNumber = memberMapper.thisWeekVisitsNumber();
                data.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long thisMonthOrderNumber = memberMapper.thisMonthOrderNumber();
                data.put("thisMonthOrderNumber",thisMonthOrderNumber);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                long thisMonthVisitsNumber = memberMapper.thisMonthVisitsNumber();
                data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                List<HotSetmeal> hotSetmeals = memberMapper.hotSetmeal();
                data.put("hotSetmeal",hotSetmeals);
            }finally {
                //减少门闩数量
                cdl.countDown();
            }
        });

        //等待门闩对象减为0，继续向下执行(等待所有任务执行完毕)
        cdl.await();


        return data;
    }
}
